package upload

import (
	"fx_swift_ifcgate_agent/pkg/code"
	"fx_swift_ifcgate_agent/pkg/logger"
	"fx_swift_ifcgate_agent/pkg/ossutils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

func BatchUpload(ctx *gin.Context) {
	// 读取前端传的多个文件
	form, _ := ctx.MultipartForm()
	files := form.File["files"]

	result := make([]string, 0)

	for _, file := range files {
		// 读取文件
		f, _ := file.Open()
		defer f.Close()

		// 获取文件扩展名
		fileExt := GetFileExt(file.Filename)
		// upload path
		path := "upload/" + file.Filename + fileExt

		// 上传到OSS
		url, err := ossutils.UploadOss(f, path)
		if err != nil {
			logger.Logger.Error("upload file to ossutils error", zap.Error(err), zap.String("path", path), zap.String("original file name", file.Filename))
			continue
		}
		result = append(result, url)
	}

	code.Success(ctx, gin.H{
		"files": result,
	})
}
